package Frame.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;

import controller.Controller;
import javax.swing.JTextArea;

public class LoginFrame extends JFrame {
	private Controller controller;
	private JPanel contentPane;
	private JPanel panel1;
	private JPanel panel2;
	private JTextField textField;
	private JTextField tf1;
	private JPasswordField tf2;
	private JButton loginButton;
	private JLabel lb2;
	private JLabel lb3;
	private JLabel lb4;
	private JLabel lb5;
	private JLabel lb6;
	public LoginFrame() {
		controller = new Controller(); // 注意 这里实例化了controller类
		controller.setLoginFrame(this);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小	
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.text);
		contentPane.setLayout(null);	
		contentPane.add(panel1());
		contentPane.add(panel2());
		contentPane.setFocusable(true);//让面板获取焦点 避免焦点在第一个文本框上	
		//contentPane.setSize(screenSize.width, screenSize.height);
		setContentPane(contentPane);
		this.setTitle("图书管理系统");
		this.setBounds(0,0,screenSize.width, screenSize.height);
		this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);//这个就是设置窗口最大化
		//this.setResizable(false);// 禁用最大化按钮
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public JPanel panel1() {
		panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		panel1.setBackground(SystemColor.text);
		panel1.setBounds(1118, 165, 538, 553);
		JLabel lb1 = new JLabel("账号登录");
		lb1.setFont(new Font("幼圆", Font.PLAIN, 30));
		lb1.setHorizontalAlignment(SwingConstants.CENTER);
		lb1.setBounds(0, 0, 538, 55);
		panel1.add(lb1);
		tf1 = new JTextField("学号/教职工号");
		tf1.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {//获取焦点
				if(tf1.getText().equals("学号/教职工号")) {
					tf1.setText("");
					tf1.setForeground(Color.BLACK);
				}
			}

			@Override
			public void focusLost(FocusEvent e) {//失去焦点
				if(tf1.getText().equals("")) { 
					tf1.setText("学号/教职工号");
					tf1.setForeground(Color.GRAY);
				}		
			}
		});
		tf1.setBounds(101, 132, 338, 48);
		tf1.setFont(new Font("幼圆", Font.PLAIN, 21));
		tf1.setColumns(10);
		panel1.add(tf1);
		
		tf2 = new JPasswordField("密码");	
		tf2.setEchoChar((char) 0);//显示原本的内容		
		tf2.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {//获取焦点
				if(new String(tf2.getPassword()).equals("密码")) {
					tf2.setText("");
					tf2.setEchoChar('·');
					tf2.setForeground(Color.BLACK);
				}
			}

			@Override
			public void focusLost(FocusEvent e) {//失去焦点
				if(new String(tf2.getPassword()).equals("")) {
					tf2.setText("密码");
					tf2.setEchoChar((char) 0);//显示原本的内容
					tf2.setForeground(Color.GRAY);
				}	
			}
		});  
		tf2.setColumns(10);
		tf2.setBounds(101, 229, 338, 48);
		tf2.setFont(new Font("幼圆", Font.PLAIN, 21));
		panel1.add(tf2);
		
		loginButton = new JButton("登录");
		loginButton.setBackground(SystemColor.textHighlight);
		loginButton.setForeground(new Color(240, 255, 240));
		loginButton.setFont(new Font("宋体", Font.PLAIN, 25));
		loginButton.setBounds(101, 341, 338, 43);
		loginButton.addActionListener(controller);
		loginButton.setActionCommand("login");
		panel1.add(loginButton);
		
		lb4 = new JLabel("注册新账号");
		lb4.setFont(new Font("宋体", Font.PLAIN, 18));
		lb4.setBounds(412, 494, 95, 30);
		lb4.addMouseListener(controller);
		lb4.setName("newAccount");
		panel1.add(lb4);
		
		lb2 = new JLabel("请输入正确的学号/教职工号");
		lb2.setForeground(new Color(255, 0, 0));
		lb2.setFont(new Font("幼圆", Font.PLAIN, 17));
		lb2.setBounds(101, 185, 223, 31);
		lb2.setVisible(false);
		panel1.add(lb2);
		
		lb3 = new JLabel("请输入正确的密码");
		lb3.setForeground(Color.RED);
		lb3.setFont(new Font("幼圆", Font.PLAIN, 17));
		lb3.setBounds(101, 280, 163, 31);
		lb3.setVisible(false);
		panel1.add(lb3);
		return panel1;
	}
	public JPanel panel2() {
		panel2 = new JPanel();
		panel2.setLayout(null);
		panel2.setBackground(SystemColor.text);
		panel2.setBounds(220, 238, 594, 374);
		lb5 = new JLabel("要知天下事，须读古人书");
		lb5.setFont(new Font("华文行楷", Font.PLAIN, 30));
		lb5.setBounds(63, 27, 341, 43);
		panel2.add(lb5);
		
		lb6 = new JLabel("—（明）冯梦龙");
		lb6.setFont(new Font("华文行楷", Font.PLAIN, 22));
		lb6.setBounds(364, 83, 170, 30);
		panel2.add(lb6);
		
		JTextArea textArea = new JTextArea();
		textArea.setFont(new Font("宋体", Font.PLAIN, 22));
		textArea.setText("“要知天下事，须读古人书” \r\n出自明代冯梦龙《醒世恒言·三孝廉让产立高名》。\r\n意思是：要想了解天下的世事沧桑，\r\n只有读通古人的书才行。");
		textArea.setBounds(65, 162, 481, 124);
		panel2.add(textArea);
		return panel2;
	}

	public JPanel getContentPane() {
		return contentPane;
	}

	public JTextField getTf1() {
		return tf1;
	}

	public void setTf1(JTextField tf1) {
		this.tf1 = tf1;
	}
	public JPasswordField getTf2() {
		return tf2;
	}

	public void setTf2(JPasswordField tf2) {
		this.tf2 = tf2;
	}

	public JLabel getLb2() {
		return lb2;
	}

	public void setLb2(JLabel lb2) {
		this.lb2 = lb2;
	}

	public JLabel getLb3() {
		return lb3; 
	}

	public void setLb3(JLabel lb3) {
		this.lb3 = lb3;
	}
	
}
